package references

import (
	"HandWritingJVM/instructions/base"
	"HandWritingJVM/instructions/constants"
	"HandWritingJVM/rtda"
	"HandWritingJVM/rtda/heap"
)

type ATHROW struct {
	base.NoOperandsInstruction
}

func (self *ATHROW) Execute(frame *rtda.Frame) {
	ex := frame.OperandStack().PopRef()
	if ex == nil {
		panic(constants.NullPointerException)
	}
	thread := frame.Thread()
	if !findAndGotoExceptionHandler(thread, ex) {
		handleUncaughtException(thread, ex)
	}
}

func handleUncaughtException(thread *rtda.Thread, ex *heap.Object) {
	//TODO
}

func findAndGotoExceptionHandler(thread *rtda.Thread, ex *heap.Object) bool {
	//TODO
	//for true {
	//	frame := thread.CurrentFrame()
	//	pc := frame.NextPC() - 1
	//	frame.Method().FindExceptionHandler(ex.Class(),pc)
	//}
	return true

}
